package com.terrier.ui.controls
{
    import com.terrier.ui.controls.sliderClasses.WxSliderBase;
    import com.terrier.ui.skins.WxSkinManager;

    import flash.geom.Point;

    /**
     * 水平滑块
     * @author Jarvis.weng
     */
    public class WxHSlider extends WxSliderBase
    {
        /**
         * 构造函数
         */
        public function WxHSlider()
        {
            super();
        }

        /**
         * @private
         */
        override public function setSkinID(skinID:String):void
        {
            setSkin(WxSkinManager.getHSliderSkin(skinID));
        }

        /**
         * @private
         */
        override protected function updateView():void
        {
            if (!isDrag)
            {
                updateThumbPosition();
            }
        }

        /**
         * @private
         */
        override protected function dragThumb(pt:Point):void
        {
            var minPos:Number = track.x;
            var maxPos:Number = track.x + track.width - thumb.width;
            var posRange:Number = maxPos - minPos;
            var xPos:Number = Math.max(minPos, Math.min(pt.x, maxPos));

            var oldValue:Number = value;
            value = (xPos - minPos) / posRange * range + minimum;

            if (oldValue != value)
            {
                updateThumbPosition();
            }

            updateToolTip();
        }

        /**
         * @private
         */
        override protected function updateThumbPosition():void
        {
            var minPos:Number = track.x;
            var maxPos:Number = track.x + track.width - thumb.width;
            var posRange:Number = maxPos - minPos;

            thumb.x = minPos + percentValue * posRange;
        }

        /**
         * @private
         */
        override protected function pointToValue(pt:Point):Number
        {
            var minPos:Number = 0;
            var maxPos:Number = track.width - thumb.width;
            var posRange:Number = maxPos - minPos;
            var xPos:Number = pt.x;

            return (xPos - minPos) / posRange * range + minimum;
        }

        /**
         * @private
         */
        public override function get width():Number
        {
            return track.width;
        }

        /**
         * @private
         */
        public override function set width(value:Number):void
        {
            track.width = value;
            updateView();
        }
    }
}
